package gui;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;

import plugin.conditions.ConditionEditor;

public class OpenUMLEditor {
    private String diapath;
    private String filepath;
    private String notationPath;
    private String projectName;

    private final static String id = "org.eclipse.papyrus.infra.core.papyrusEditor";
    /**
     * @return the diapath
     */
    public String getDiapath() {
            return diapath;
    }
    /**
     * @param diagrampath the diapath to set
     */
    public void setDiapath(String diagrampath) {
            this.diapath = diagrampath;
    }
    /**
     * @return the filepath
     */
    public String getFilepath() {
            return filepath;
    }
    /**
     * @param filepath the filepath to set
     */
    public void setFilepath(String filepath) {
            this.filepath = filepath;
    }
    /**
     * @return the notationPath
     */
    public String getNotationPath() {
        return notationPath;
    }
    /**
     * @param notationPath the notationPath to set
     */
    public void setNotationPath(String notationPath) {
        this.notationPath = notationPath;
    }
    /**
     * @return the projectName
     */
    public String getProjectName() {
        return projectName;
    }
    /**
     * @param projectName the projectName to set
     */
    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }
    public OpenUMLEditor(String dfilepath, String fpath, String notationPath, String projectName)
    {
            this.setDiapath(dfilepath);
            this.setFilepath(fpath);
            this.setNotationPath(notationPath);
            this.setProjectName(projectName);
    }
    public void open()
    {
        File destFolder = new File(ConditionEditor.projectPath + File.separator + "temp");
        if(!destFolder.exists()) {
            destFolder.mkdirs();
        }
                    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
                    IProject project = workspaceRoot.getProject(projectName);
                    if(!project.exists())
                            try {
                                    project.create(null);
                            } catch (CoreException e2) {
                                    // TODO Auto-generated catch block
                                    e2.printStackTrace();
                            }
                    if(!project.isOpen())
                    {
                                    try {
                                            project.open(null);
                                    } catch (CoreException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                    }
                    }
                    IPath location = project.getLocation();
                    String locationString = location.toOSString();
                    System.out.println(locationString);
                    /***********************************/
                    final IPath diagrampath = new Path(diapath);
                    final IPath umlpath = new Path(filepath);
                    final IPath notPath = new Path(notationPath);
                    
                    /**********************************/
                    //final IFile file = project.getFile(mypath);
                    final IFile diagramfile = project.getFile(diagrampath.lastSegment());
                    final IFile umlfile = project.getFile(umlpath.lastSegment());
                    final IFile noteFile = project.getFile(notPath.lastSegment());
                    //System.out.println(file.getLocation().toString());
            
                    try
                    {
                     final IWorkbench wb = PlatformUI.getWorkbench();
                     wb.getDisplay().asyncExec(new Runnable(){

                                    @Override
                                    public void run() {
                                             
                                            // TODO Auto-generated method stub
                                            IWorkbenchWindow window = wb.getActiveWorkbenchWindow();
                                            if(window != null)
                                            {
                                                    IWorkbenchPage pg = window.getActivePage();
                                                    try {
                                                            diagramfile.createLink(diagrampath,IResource.REPLACE,null);
                                                            umlfile.createLink(umlpath, IResource.REPLACE,null);
                                                            noteFile.createLink(notPath, IResource.REPLACE, null);
                                                            pg.openEditor(new FileEditorInput(diagramfile), id);
                                                    } catch (Exception e) {
                                                            // TODO Auto-generated catch block
                                                            e.printStackTrace();
                                                    }
                                            }
                                    }
                     }
                     );
                    } catch (Exception e) 
                    {
                            System.out.println(e.getMessage());
                    }
    }
}
